<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$idx = new Usercp;
class Usercp
{
	var $seller_data;

	function Usercp()
	{
		global $INCOME,$BIZ;
        define("PAGE_NUM",10);
		$BIZ -> assign("code",$INCOME[code]);	
		$this -> seller_data = new manager($BIZ->seller['seller_id']);
	    $BIZ -> nav_link[] = array("商家控制台","index.php?act=Seller");

		switch($INCOME[code])
		{
			case '04':     //modify seller info
			    $this -> seller_modify_form();
			    $BIZ-> nav_link[] = array("修改个人信息","");
			    break;
			case '03':     //do modify seller info
			    $this -> do_seller_modify();
			    break;
			case '05':    //password change form
				$BIZ-> nav_link[] = array("更改登陆密码");
			    break;
			case '06':   // do passy change
				$this -> do_pass();
				break;
			case 'req':
				$BIZ-> nav_link[] = array("发布求购信息","");
			    break;
			case 'viewreq':
			    $BIZ-> nav_link[] = array("管理求购信息","");
			    $this -> view_req();
				break;
			case 'doreq':
				$this -> add_req();
			   break;
			case 'mdreq':
				$this -> mod_req();
			   break;
			 case 'domdreq': 
				$this -> do_modify_req();
			   break;
			case 'up':
				$this -> up_req();   //提前
			   break;
			case 'delr':
			     $this -> del_req();
			      break;
			case 'clog':
				 $BIZ-> nav_link[] = array("查看帐户","");
				  $this -> clog_form();  //消费明晰
				  break;
		    case 'delog':
				$this -> del_log();
			    break;
			case 'buy': 
				$BIZ-> nav_link[] = array("等级购买"); 
				$this -> buy_form();
			    break;
			case 'dobuy':
				$this -> do_buy();
			    break;
			case 'upgrade':
				$BIZ-> nav_link[] = array("等级升级");
				$this -> upgrade_form();
			    break;
			case 'doupgrade':
				$this -> do_upgrade();
			    break;
			case 'price':
				$BIZ -> nav_link[] = array("消费价目一揽");
			    $this -> price_form();
				break;
			case 'dobuypic':
				$this -> do_buy_pic();
				break;
			default:
				break;
		}

		$BIZ -> output("User.tpl");
	}

	function seller_modify_form()   
	{   //04
		global $BIZ,$DB;
		$DB -> db_query( "SELECT * FROM biz_seller WHERE seller_id='".$BIZ->seller['seller_id']."' LIMIT 1" );
		$res = $DB-> db_fetch_row();
		$BIZ -> assign('mod',$res);
		return;
    }
	function do_seller_modify()  
	{   //03
		global $BIZ,$DB,$INCOME;
		$su = $INCOME['modf'];
		$DB -> db_query( "SELECT count(*) as ct FROM biz_seller WHERE seller_id = '". $BIZ->seller['seller_id']."'" );
		$s_re = $DB -> db_fetch_row();
		if( !$s_re['ct'] ) $BIZ ->error("数据查询错误","?act=Seller");
		$DB -> db_form_update('biz_seller',$su,"seller_id = '". $BIZ->seller['seller_id']."'");
	    $BIZ ->redirect_screen("您已经成功更新了个人信息","?act=Seller");
	}
	
	function do_pass()
	{   //06
		global $BIZ,$DB,$INCOME;

		if( $INCOME['new_pass'] != $INCOME['new_again'] )
			$BIZ -> error("两次密码不一致","?act=Seller&code=05");
		
		$old_p =md5($INCOME['old_pass']);
        $new_p =md5($INCOME['new_pass']);

		$DB -> db_query( "SELECT seller_password as p FROM biz_seller WHERE seller_id='".$BIZ->seller['seller_id']."' LIMIT 1" );

		$res = $DB -> db_fetch_row();

		if( $res[p] != $old_p )
			$BIZ -> error("旧密码错误","?act=Seller&code=05");

		$DB -> db_query( "UPDATE biz_seller SET seller_password = '$new_p' WHERE seller_id='".$BIZ->seller['seller_id']."' LIMIT 1" );

		$BIZ ->redirect_screen("恭喜！您已经成功修改了密码","?act=Seller");
	}

	function add_req()
	{
		global $INCOME,$DB,$BIZ;

		$lre=$INCOME['re'];
		$lre['info_active_time'] = time();
		$lre['info_writer']=$BIZ -> seller['seller_logaccount'];
		$lre['info_owner_id']=$BIZ -> seller['seller_id'];
		$lre['info_type'] = INFO_REQ;

		$DB->db_form_insert("biz_info",$lre);
        $BIZ ->redirect_screen("恭喜！您已经成功发布了一条求购信息","?act=User&code=viewreq");	
	}
	function view_req()
	{
		global $INCOME,$DB,$BIZ;

		$DB -> db_query("SELECT * FROM biz_info WHERE info_owner_id=".$BIZ->seller['seller_id']." AND info_type=".INFO_REQ." ORDER BY info_active_time DESC");
        while($r=$DB -> db_fetch_row() )
		{
			$tm = date("Y-m-d H:i",$r['info_active_time']);
			$re[] = array($r[info_title],$tm,$r[id]);
		}
        $BIZ -> assign("re",$re);

		return;
	}
	function mod_req()
	{
		global $INCOME,$DB,$BIZ;
		$id = intval($INCOME['RID']);
		$DB -> db_query("SELECT * FROM biz_info WHERE id=".$id." LIMIT 1");
		$re = $DB -> db_fetch_row();
		$BIZ ->assign("re",$re);
        return;
	}

	function do_modify_req()
	{
		global $INCOME,$DB,$BIZ;
		$rid = intval($INCOME['RID']);
		$re= $INCOME['re'];

		$DB -> db_form_update("biz_info",$re,"id=".$rid);
        $BIZ ->redirect_screen("恭喜！您已经成功更新了求购信息","?act=User&code=viewreq");	

	}

	function up_req()
	{
		global $INCOME,$DB,$BIZ,$feedb;
		$res = $this -> seller_data ->consume($feedb[up_req]);
		if(  $res<0 )
		{
			$m=-$res;
			$BIZ -> error("提示：您的可用消费金额已不足！本次消费您还需要{$m}点。请立即冲值。");    
		}
		$this -> seller_data -> consume_log(UP_REQ_FEE,"提前求购信息排序",$BIZ ->seller[seller_id]);
		$rid = intval($INCOME['RID']);
		$rd['info_active_time'] = time();
		$DB -> db_form_update("biz_info",$rd,"id=".$rid);
        $BIZ ->redirect_screen("恭喜！您已经成功提前了求购信息。消费".$feedb[up_req]."点。剩余$res点。","?act=User&code=viewreq");	
	}
	function del_req()
	{//34
		global $DB,$BIZ,$INCOME;
		$rid = intval($INCOME['RID']);
		$DB -> db_query("DELETE FROM biz_info WHERE id=".$rid);
        $BIZ ->redirect_screen("恭喜！您已经成功删除了一条求购信息","?act=User&code=viewreq");	
	}
    function clog_form()
	{
		global $DB,$BIZ,$INCOME;
		
		$start=$start2=0;
		//消费
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		$DB -> db_query("SELECT count(*)as count FROM biz_consume_log WHERE seller_id=".$BIZ -> seller['seller_id']);

		$row_count = $DB -> db_fetch_row();
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=User"."&code={$INCOME['code']}"
											   )
											);

		$DB -> db_query("SELECT * FROM biz_consume_log WHERE seller_id=".$BIZ -> seller['seller_id']." ORDER BY time DESC LIMIT $start,".PAGE_NUM);
		while ($r = $DB -> db_fetch_row() )
		{
			$tm = date("Y-m-d H:i",$r['time']);
			$clog[] =array($r['act'],$tm,$r['money'],$r['id']);
		}
		//余额
		$money = $this -> seller_data -> get_money();
		//冲值

        $DB -> db_query("SELECT * FROM biz_charge_log WHERE sid=".$BIZ -> seller['seller_id']." ORDER BY time DESC ");	
		while( $r=$DB -> db_fetch_row())
		{
			$tm = date("Y-m-d H:i",$r['time']);
			$remark = $r['operator']?"购买":"系统赠送";
			$alog[] =array($tm,$r['money'],$r['id'],$remark);
		}
		$BIZ -> assign("page",$pages);
		$BIZ -> assign("total",$money);
		$BIZ -> assign("clog",$clog);
		$BIZ -> assign("alog",$alog);
	}
	function del_log()
	{
		global $DB,$BIZ,$INCOME;
		$id_data= @implode(",",$INCOME[delog]);
		if($id_data)
		{
		$DB -> db_query("DELETE FROM biz_consume_log WHERE id IN (".$id_data.")");
        $BIZ ->redirect_screen("恭喜！您已经成功删除了消费明细","?act=User&code=clog");	
		}else
			$BIZ -> error("错误：未选定删除记录");
	}
	function buy_form()
	{
		global $DB,$BIZ;
		$data = $this -> seller_data-> get_seller_data("property_id,end_time");
		if($data['property_id']==1) $BIZ ->error("注意：您目前为免费用户，不需要冲值。您可以选择升级为更高级别的商家，享受更多的服务。谢谢！");
		$BIZ ->assign("level",$this -> seller_data->get_level_name());
		$tm  = $data['end_time']?$data['end_time']:time();
		$BIZ ->assign("end_time",date("Y-m-d H:i",$tm) );
	}
	function do_buy()
	{
		global $INCOME,$DB,$BIZ;

		$month = intval($INCOME['period']);
		$money = $month * $this -> seller_data->get_fee();
		$res = $this -> seller_data ->consume($money);
		if(  $res <0 ) $BIZ -> error("提示：您的可用消费金额已不足！本次消费您还需要{$res}点");
		$this -> seller_data -> consume_log($money,"购买商家等级",$BIZ ->seller[seller_id]);
		$result = $this -> seller_data->update_end_time($month*30*24*3600);
		if($result&&$BIZ->seller['seller_check_status']=='uncheck')
		{
			//送启动资金
			if(!$this -> seller_data->init_fee()) $BIZ->error("错误：启动资金赠送失败！");
			$DB -> db_query("UPDATE biz_seller SET seller_check_status ='checked' WHERE seller_id=".$BIZ->seller[seller_id]);
		}
		$BIZ ->redirect_screen("恭喜！您已经成功地购买了商家等级，本次消费共花费{$money}点，剩余{$res}点。","?act=Seller");

	}
	function upgrade_form()
	{
		global $DB,$BIZ;
		$BIZ ->assign("level",$this -> seller_data->get_level_name());
		$data = $this -> seller_data->get_seller_data("end_time,property_id");
		$tm = $data['end_time']?$data['end_time']:time();
		$BIZ ->assign("end_time",date("Y-m-d H:i",$tm) );
		$DB -> db_query("SELECT * FROM biz_seller_property WHERE rank_id>".$this -> seller_data->get_rank());
		if( !$DB -> db_fetch_num() )
			$BIZ -> error("对不起，您已经是系统设定地最高级别，无需升级。您可以选择续费延长使用日期。");
		
		while( $r=$DB -> db_fetch_row() )
		{
			$level_id[] = $r['level_id'];
		    $level_name[] = $r['property_name'];
		}
		$BIZ -> assign("level_id",$level_id);
		$BIZ -> assign("level_name",$level_name);
	}
	function do_upgrade()
	{
		global $DB,$BIZ,$INCOME;
		$one = $this -> seller_data->get_fee($INCOME['tolevel'])-$this -> seller_data->get_fee();	
		$end_time = $this -> seller_data->get_end_time();
		if( ($long = $end_time- time()) <0 ) 
		{
			$long = 3*30*24*3600; // 最低升级时间长度是一个季
			$this -> seller_data->update_end_time($long);
		}
		$money = $long * $one;
		$money = ceil($money/(24*30*3600));
		$res = $this -> seller_data ->consume($money);
		if( $res<0 ) $BIZ -> error("提示：您的可用消费金额已不足！本次消费您还需要{$res}点");	     $this -> seller_data -> consume_log($money,"升级商家等级",$BIZ ->seller[seller_id]);
		$buy_pic=$this -> seller_data->get_buy_pic();
        $buy_pic<0 && $buy_pic=0;
		$rs = $DB -> db_once("SELECT att_pic_total FROM biz_seller_property WHERE level_id=".$INCOME['tolevel']." LIMIT 1");
        $total_pic=$rs['att_pic_total']+$buy_pic;
		$DB -> db_query("UPDATE biz_seller_data SET property_id =".$INCOME['tolevel'].", pic_allow=".$total_pic." WHERE seller_id=".$BIZ->seller[seller_id]);
	   	$BIZ ->redirect_screen("恭喜！您已经成功地升级了商家等级，本次消费共花费{$money}点。剩余{$res}点","?act=Seller");
	}
	function price_form()
	{
		global $BIZ,$feedb,$_LEVEL;
		$BIZ -> assign("feedb",$feedb);
		foreach($_LEVEL as $v)
			$lev[] = array($v[0],$v[1]);
		$BIZ -> assign("level",$lev);
	}
	function do_buy_pic()
	{
		global $BIZ,$DB,$feedb,$INCOME;

		$money = $feedb['pic_num']*intval($INCOME['buy_num']);
		$res = $this -> seller_data ->consume($money);
		if(  $res<0 ) $BIZ -> error("提示：您的可用消费金额已不足！你还差{$res}点");
		$this -> seller_data -> consume_log($money,"购买副图位",$BIZ ->seller[seller_id]);

		$DB -> db_query("UPDATE `biz_seller_data` SET pic_allow=pic_allow+".intval($INCOME['buy_num'])." WHERE seller_id=".$BIZ->seller['seller_id']);

		$BIZ ->redirect_screen("恭喜！您已经成功地购买了图片位。共消费{$money}点","?act=Seller&code=16");

	}
}
?>